package com.hxzy.tms.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hxzy.tms.Assert;
import com.hxzy.tms.dao.ProjectDao;
import com.hxzy.tms.dao.TeamDao;
import com.hxzy.tms.dao.UserDao;
import com.hxzy.tms.entity.Project;
import com.hxzy.tms.entity.Team;
import com.hxzy.tms.entity.User;
import com.hxzy.tms.service.ProjectService;
import com.hxzy.tms.service.ServiceResult;

public class DefaultProjectService implements ProjectService {
    private ProjectDao proDao;
    private TeamDao teamDao;
    private UserDao userDao;

    public ServiceResult<Project> createProject(Project project) {
        Assert.notBlank(project.getTitle(), "项目组名称为空");
        Assert.notBlank(project.getDesc(), "项目组描述为空");
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("title", project.getTitle());
        List<Project> projects = null;
        try {
            projects = proDao.read("from Project where title=:title", param);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (projects.get(0) != null) {
            return new ServiceResult<Project>(-2, null, "存在相同的项目名");
        }
        Project pro = proDao.create(project);
        return new ServiceResult<Project>(1, pro, "发布任务成功");
    }

    public List<Project> showAllProject(Team team) {
        List<Project> projects = proDao.readMyProject(team.getId());
        for (Project project : projects) {
            teamDao.read(Team.class, project.getTeamId());
        }
        return null;
    }

    public List<Map<String, String>> showMyProject(User user) {
        List<Map<String, String>> list = new ArrayList<Map<String, String>>();
        Map<String, String> param = new HashMap<String, String>();
        List<Project> projects = proDao.readTeamProject(user.getId());
        for (Project project : projects) {
            param.put("分组名称", project.getTitle());
            param.put("团队名称", teamDao.read(Team.class, project.getTeamId()).getName());
            param.put("分组描述", project.getDesc());
            list.add(param);
            // param.put("创建者", project.getUidCreate().getNickname());
            // param.put("修改者", userDao.read(User.class,
            // project.getUidModified()).getNickname());
            // param.put("修改时间",
            // InfoFilter.timeToString(project.getGmtModified()));
            // param.put("创建时间",
            // InfoFilter.timeToString(project.getGmtCreate()));
        }
        return list;
    }

    public ServiceResult<Project> changeProject(Project project) {
        int status = proDao.merge(project);
        ServiceResult<Project> result = null;
        if (status == 1) {
            result = new ServiceResult<Project>(1, project, "修改成功");
        } else {
            result = new ServiceResult<Project>(2, project, "修改失败");
        }
        return result;
    }

    public Project getNowProject(Long id) {
        Project project = proDao.read(Project.class, id);
        return project;
    }

    public ProjectDao getProDao() {
        return proDao;
    }

    public void setProDao(ProjectDao proDao) {
        this.proDao = proDao;
    }

    public TeamDao getTeamDao() {
        return teamDao;
    }

    public void setTeamDao(TeamDao teamDao) {
        this.teamDao = teamDao;
    }

    public UserDao getUserDao() {
        return userDao;
    }

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

}
